const app = getApp();
import {getNavBanner} from "../../../config/api";
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model:1,
    topNum:0,
    lists: [],
    keywords: '',
    page: 1,
    limit: 10,
    isEmpty: false, // 是否为空
    isBottom: false, // 是否无数据触底了
    isReachBtm: false, // 是否触发上拉加载
    showBackTop:false,
    banner:null,//顶部图片
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let model = wx.getStorageSync('model')
    if (model) {
      this.setData({
        model
      })
    }
    this.getList();
    if(options.item){
      console.log('options.item:',JSON.parse(options.item));
      let item = JSON.parse(options.item);
       options.item?this.getBanner(item.id):console.log('没有id不用调用顶部图片');
   }
  },


  //获得顶部图片
  getBanner(id){
    let that = this;
    console.log('调用顶部图片的栏目id是：',id);
    util.apiRequest(getNavBanner,{id:id},null).then(r=>{
      console.log('获得顶部栏目图片为：',r);
      that.setData({
        banner:r.data.data?r.data.data.banner:null
      });
    });
  },

   

  getList() {
    let that = this;
    wx.request({
      url: 'http://zhaopintong.yuehuio.com//api/External/job_list',
      data: {
        keywords: this.data.keywords,
        page: this.data.page,
        limit: this.data.limit
      },
      header: {
        'content-type': 'application/json'
      },
      success(r) {
        console.log('招聘：', r)
        var newList = that.data.lists.concat(r.data.data)
        if (r.data.data.length == 0 && that.data.isReachBtm == true) {
          that.setData({
            isBottom: true
          })
          wx.showToast({
            title: '暂无更多',
            icon: 'none',
          })
        }

        that.setData({
          lists: newList
        })

        if (newList.length == 0) {
          that.setData({
            isEmpty: true
          })
        } else {
          that.setData({
            isEmpty: false
          })
        }
      }
    })
  },

  searchInput(e) {
    console.log(e)
    this.setData({
      keywords: e.detail.value,
      page: 1,
      lists: []
    })
    this.getList()
  },

  toDetail(e){
    wx.navigateTo({
      url: '/pages/new_index/job_list/job_detail/job_detail?id='+e.currentTarget.dataset.id,
    })
  },

  scrollView(e){
   // console.log("666:",e)
    if (e.detail.scrollTop > 100){
      this.setData({
        showBackTop:true
      })
    }else{
      this.setData({
        showBackTop: false
      })
    }
  },

  returnTop: function () {
    this.setData({
      topNum:0
    });
  },

  lower(){
    var that = this;
    var page = this.data.page;
    that.setData({
      isReachBtm: true
    })
    if (that.data.isBottom) return;
    page++;
    that.setData({
      page
    })
    console.log('page++', page)
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function() {
  //   var that = this;
  //   var page = this.data.page;
  //   that.setData({
  //     isReachBtm: true
  //   })
  //   if (that.data.isBottom) return;
  //   page++;
  //   that.setData({
  //     page
  //   })
  //   console.log('page++', page)
  //   this.getList()
  // },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})